package com.example.bruger.androidneurosky;

import android.app.FragmentManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;


public class TrafficlightActivity extends ActionBarActivity implements TGCallBack {
    boolean won = false;

    ImageView redView;
    ImageView yellowView;
    ImageView greenView;
    TextView points;
    Button startBtn;

    Timer timer;
    long startTime = 0;
    long totalTime = 0;
    Handler timerHandler = new Handler();
    TextView timeTextView;


    boolean done = false;
    boolean running = true;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trafficlight);
        TGHandler.SetNewCallBack(this);


        startBtn = (Button) findViewById(R.id.buttonStart);
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startGame();
            }
        });
        redView = (ImageView) findViewById(R.id.imageViewRed);
        yellowView = (ImageView) findViewById(R.id.imageViewYellow);
        greenView = (ImageView) findViewById(R.id.imageViewGreen);

        points = (TextView) findViewById(R.id.textViewPoint);
        points.setText("Time to finish: ");

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_brainwave, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



    public void Update(int attention, int meditation, int delta, int theta,
                       int lowAlpha, int highAlpha, int lowBeta, int highBeta,
                       int lowGamma, int midGamma) {
        ((TextView) findViewById(R.id.textViewAttention)).setText("Attention: " + attention + " - " + attention + " %");

        if(attention>90) {
            won = true;
        }

    }

    public void startGame() {

            startBtn.setVisibility(View.INVISIBLE);
            timeTextView = (TextView) findViewById(R.id.timeTextView);

            startTime = System.currentTimeMillis();
            timer = new Timer();
            startTime = System.currentTimeMillis();
            timerHandler.postDelayed(timerRunnable, 0);

            redView.setVisibility(View.INVISIBLE);
            yellowView.setVisibility(View.INVISIBLE);
            greenView.setVisibility(View.INVISIBLE);


    }

    Runnable timerRunnable = new Runnable() {

        @Override
        public void run() {
            totalTime = System.currentTimeMillis() - startTime;
            int seconds = (int) (totalTime / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;
            timeTextView.setText(String.format("%02d:%02d", minutes, seconds));


            timerHandler.postDelayed(this, 500);

            if(running==true) {
                if(seconds==2) {
                    redView.setVisibility(View.VISIBLE);
                }
                else if(seconds==4) {
                    yellowView.setVisibility(View.VISIBLE);
                    redView.setVisibility(View.INVISIBLE);
                }
                else if(seconds==6) {
                    greenView.setVisibility(View.VISIBLE);
                    yellowView.setVisibility(View.INVISIBLE);
                    running=false;
                }
            }

            if(won==true && running==false && done ==false)
            {
                done=true;
                points.setText("Time to finish: "+String.format("%02d:%02d", minutes, seconds));
                redView.setVisibility(View.VISIBLE);
                yellowView.setVisibility(View.VISIBLE);

                timerHandler.removeCallbacks(timerRunnable);
            }

        }
    };
}
